﻿#include "msgapi.h"

MsgApi::MsgApi() {

}

bool MsgApi::getJsonObject(const QByteArray &json, const QString &key, QJsonObject &oValue) {
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(json, &jsonError);  // 转化为 JSON 文档
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) {  // 解析未发生错误
        if (doucment.isObject()) {  // JSON 文档为对象
            QJsonObject object = doucment.object();  // 转化为对象
            if (object.contains(key)) {
                QJsonValue value = object.value(key);
                if (value.isObject()) {  // result 的 value 是对象
                    oValue = value.toObject();
                    return true;
                }
            }
        }
    }
    return false;
}

bool MsgApi::getJsonArray(const QByteArray &json, const QString &key, QJsonArray &aValue) {
    QJsonParseError jsonError;
    QJsonDocument doucment = QJsonDocument::fromJson(json, &jsonError);  // 转化为 JSON 文档
    if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) {  // 解析未发生错误
        if (doucment.isObject()) {  // JSON 文档为对象
            QJsonObject object = doucment.object();  // 转化为对象
            if (object.contains(key)) {
                QJsonValue value = object.value(key);
                if (value.isArray()) {  // result 的 value 是对象
                    aValue = value.toArray();
                    return true;
                }
            }
        }
    }
    return false;
}

bool MsgApi::getJsonInt(const QJsonObject &object, const QString &key, int &iValue) {
    if (object.contains(key)) {
        QJsonValue value = object.value(key);
        if (value.isDouble()) {
            iValue = value.toVariant().toInt();
            return true;
        }
    }
    return false;
}

bool MsgApi::getResult(const QJsonObject &object, int &reult) {
    if(object.value("data").isObject()) {
        QJsonObject oData = object.value("data").toObject();
        reult = oData.value("result").toInt();
        return true;
    }
    return false;
}


bool MsgApi::isConnected() {
    return TcpClient::getClient()->isConnected();
}

bool MsgApi::insertAttendanceRecord(QString &userId, int attWay, QString &picPath, QString &currentTime) {
    QJsonObject data;
    data.insert("msg_type", QJsonValue(MSG_ATT_INSERT_RECORD));
    data.insert("user_id", QJsonValue(userId));
    data.insert("att_way", QJsonValue(attWay));
    data.insert("face_pic_path", QJsonValue(picPath));
    data.insert("create_time", QJsonValue(currentTime));

    QJsonDocument doc(data);
    QByteArray dataArray = doc.toJson();
    QString strData = dataArray;

    if(!TcpClient::getClient()->sendData(strData)) {
        return false;
    }

    return true;
}

bool MsgApi::parseInsertAttendanceRecord(QJsonObject &object, int &result, int &count, QString &createTime) {
    QJsonObject oJson;
    if(!object.value("data").isObject()) {
        return false;
    }

    oJson = object.value("data").toObject();
    if(oJson.value("result").isNull() || !oJson.value("result").isDouble()) {
        return false;
    }
    result = oJson.value("result").toInt();

    if(oJson.value("create_time").isNull() || !oJson.value("create_time").isString()) {
        return false;
    }
    createTime = oJson.value("create_time").toString();

    if(oJson.value("count").isNull() || !oJson.value("count").isDouble()) {
        return false;
    }
    count = oJson.value("count").toInt();

    return true;
}

bool MsgApi::getAttendanceInfoList(int page, int pageSize, QString currentSearchUsername, QString currentSearchStartTime, QString currentSearchEndTime) {
    QJsonObject data;
    data.insert("msg_type", QJsonValue(MSG_ATT_INFO_LIST));
    data.insert("page", QJsonValue(page));
    data.insert("page_size", QJsonValue(pageSize));
    if(!currentSearchUsername.isEmpty()) {
        data.insert("username", QJsonValue(currentSearchUsername));
    } else {
        data.insert("username", QJsonValue(""));
    }
    if(!currentSearchStartTime.isEmpty()) {
        data.insert("start_time", QJsonValue(currentSearchStartTime));
    } else {
        data.insert("start_time", QJsonValue(""));
    }
    if(!currentSearchEndTime.isEmpty()) {
        data.insert("end_time", QJsonValue(currentSearchEndTime));
    } else {
        data.insert("end_time", QJsonValue(""));
    }

    QJsonDocument doc(data);
    QByteArray dataArray = doc.toJson();
    QString strData = dataArray;

    if(!TcpClient::getClient()->sendData(strData)) {
        return false;
    }

    return true;
}

bool MsgApi::parsegetAttendanceInfoList(QJsonObject &object, int &count, QList<AInfo> &attendanceInfoList) {
    if(!object.value("data").isObject()) {
        return false;
    }
    QJsonObject dataObject = object.value("data").toObject();
    if(dataObject.value("count").isNull() || !dataObject.value("count").isDouble()) {
        return false;
    }
    count = dataObject.value("count").toInt();

    QJsonArray aJson;
    if(!dataObject.value("attendanceInfoList").isArray()) {
        return false;
    }
    aJson = dataObject.value("attendanceInfoList").toArray();
    for (int i = 0; i < aJson.count(); i++) {
        AInfo aInfo;
        QJsonValue value = aJson.at(i);
        aInfo.userId = aJson.at(i).toObject().value("user_id").toString();
        aInfo.username = aJson.at(i).toObject().value("username").toString();
        aInfo.attWay = aJson.at(i).toObject().value("att_way").toString();
        aInfo.picPath = aJson.at(i).toObject().value("face_pic_path").toString();
        aInfo.createTime = aJson.at(i).toObject().value("create_time").toString();
        attendanceInfoList.push_back(aInfo);
    }
    return true;
}

bool MsgApi::userRegister(UInfo &uInfo) {
    QJsonObject data;
    data.insert("msg_type", QJsonValue(MSG_USER_REGISTER));
    data.insert("user_id", QJsonValue(uInfo.userId));
    data.insert("username", QJsonValue(uInfo.username));
    data.insert("pic_path", QJsonValue(uInfo.picPath));
    data.insert("rfid_card_no", QJsonValue(uInfo.rfidCardNo));
    data.insert("create_time", QJsonValue(uInfo.createTime));

    QJsonDocument doc(data);
    QByteArray dataArray = doc.toJson();
    QString strData = dataArray;

    if(!TcpClient::getClient()->sendData(strData)) {
        return false;
    }

    return true;
}

bool MsgApi::parseUserRegister(QJsonObject &object, int &result, int &count) {
    QJsonObject oJson;
    if(!object.value("data").isObject()) {
        return false;
    }
    oJson = object.value("data").toObject();

    if(oJson.value("result").isNull()) {
        return false;
    }
    result = oJson.value("result").toInt();

    if(oJson.value("count").isNull()) {
        return false;
    }
    count = oJson.value("count").toInt();

    return true;
}

bool MsgApi::getUserInfoList(int page, int pageSize, QString currentSearchUsername,
                             QString currentSearchStartTime, QString currentSearchEndTime) {
    QJsonObject data;
    data.insert("msg_type", QJsonValue(MSG_USER_INFO_LIST));
    data.insert("page", QJsonValue(page));
    data.insert("page_size", QJsonValue(pageSize));
    if(!currentSearchUsername.isEmpty()) {
        data.insert("username", QJsonValue(currentSearchUsername));
    } else {
        data.insert("username", QJsonValue(""));
    }
    if(!currentSearchStartTime.isEmpty()) {
        data.insert("start_time", QJsonValue(currentSearchStartTime));
    } else {
        data.insert("start_time", QJsonValue(""));
    }
    if(!currentSearchEndTime.isEmpty()) {
        data.insert("end_time", QJsonValue(currentSearchEndTime));
    } else {
        data.insert("end_time", QJsonValue(""));
    }

    QJsonDocument doc(data);
    QByteArray dataArray = doc.toJson();
    QString strData = dataArray;

    if(!TcpClient::getClient()->sendData(strData)) {
        return false;
    }

    return true;
}

bool MsgApi::parseGetUserInfoList(QJsonObject &object, int &count, QList<UInfo> &userInfoList) {
    if(!object.value("data").isObject()) {
        return false;
    }
    QJsonObject dataObject = object.value("data").toObject();
    if(dataObject.value("count").isNull()) {
        return false;
    }
    count = dataObject.value("count").toInt();

    QJsonArray aJson;
    if(!dataObject.value("userInfoList").isArray()) {
        return false;
    }
    aJson = dataObject.value("userInfoList").toArray();
    for (int i = 0; i < aJson.count(); i++) {
        UInfo uInfo;
        QJsonValue value = aJson.at(i);
        uInfo.userId = aJson.at(i).toObject().value("user_id").toString();
        uInfo.username = aJson.at(i).toObject().value("username").toString();
        uInfo.picPath = aJson.at(i).toObject().value("pic_path").toString();
        uInfo.rfidCardNo = aJson.at(i).toObject().value("rfid_card_no").toString();
        uInfo.createTime = aJson.at(i).toObject().value("create_time").toString();
        userInfoList.push_back(uInfo);
    }
    return true;
}

bool MsgApi::userDelete(QString &userId) {
    QJsonObject data;
    data.insert("msg_type", QJsonValue(MSG_USER_DELETE));
    data.insert("user_id", QJsonValue(userId));

    QJsonDocument doc(data);
    QByteArray dataArray = doc.toJson();
    QString strData = dataArray;

    if(!TcpClient::getClient()->sendData(strData)) {
        return false;
    }

    return true;
}

bool MsgApi::parseUserDelete(QJsonObject &object, int &result, int &count) {
    QJsonObject oJson;
    if(!object.value("data").isObject()) {
        return false;
    }
    oJson = object.value("data").toObject();

    if(oJson.value("result").isNull()) {
        return false;
    }
    result = oJson.value("result").toInt();

    if(oJson.value("count").isNull()) {
        return false;
    }
    count = oJson.value("count").toInt();

    return true;
}

bool MsgApi::getUserInfoCount(int &count) {
    QJsonObject data;
    data.insert("msg_type", QJsonValue(MSG_USER_INFO_COUNT));

    QJsonDocument doc(data);
    QByteArray dataArray = doc.toJson();
    QString strData = dataArray;

    if(!TcpClient::getClient()->sendData(strData)) {
        return false;
    }

    QString readBuf;
    if(!TcpClient::getClient()->readData(readBuf)) {
        return false;
    }

    QJsonArray result;
    if(!getJsonArray(readBuf.toLocal8Bit(), "data", result)) {
        return false;
    }

//    for (int i = 0; i < result.count(); i++) {
//        QJsonValue value = result.at(i);
//    }
    if(!getJsonInt(result.at(0).toObject(), "count", count)) {
        return false;
    }

    qDebug() << "count : " << count;

    return true;
}
